package com.antrain.utils;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Set;

import com.antrain.core.Mapper;

@SuppressWarnings("rawtypes")
public class JDBCUtil {
    private static String driverClass = "";
    private static String url = "";
    private static String username = "";
    private static String password = "";

    public static void init(Map<String, String> propConfig) {
        url = propConfig.get("url");
        driverClass = propConfig.get("driverClass");
        username = propConfig.get("username");
        password = propConfig.get("password");
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            Class.forName(driverClass);
            connection = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动加载失败");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        return connection;
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (connection != null)
                connection.close();
            if (statement != null)
                statement.close();
            if (resultSet != null)
                resultSet.close();
        } catch (SQLException e) {
            System.out.println("数据库关闭异常");
            e.printStackTrace();
        }
    }

    public static void close(Connection connection, PreparedStatement statement) {
        try {
            if (connection != null)
                connection.close();
            if (statement != null)
                statement.close();
        } catch (SQLException e) {
            System.out.println("数据库关闭异常");
            e.printStackTrace();
        }
    }

    public static void close(Connection connection, PreparedStatement statement, ResultSet resultSet) {
        try {
            close(connection, statement);
            if (resultSet != null)
                resultSet.close();
        } catch (SQLException e) {
            System.out.println("数据库关闭异常");
            e.printStackTrace();
        }
    }

    public static int executeUpdate(String sql) {
        Connection connection = getConnection();
        PreparedStatement statement = null;
        try {
            statement = connection.prepareStatement(sql);
            return statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement);
        }
        return 0;
    }

    public static Object excuteQuery(String sql, Class clz, Mapper mapper){
        Object object = null;
        Connection connection = getConnection();
        PreparedStatement statement = null;
        ResultSet resultSet = null ;
        try {
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                // 创建一个对象，目前属性的值都是初始值
                object = clz.newInstance();
                Map<String,String> propMap = mapper.getPropMapper();
                Set<String> keySet = propMap.keySet();
                for(String prop : keySet){
                    String colunm = propMap.get(prop);
                    Field field = clz.getDeclaredField(prop);
                    field.setAccessible(true);
                    field.set(object, resultSet.getObject(colunm));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(connection, statement,resultSet);
        }
        return object;
    }

    // 测试数据库连接是否成功
    public static void main(String[] args) {
        Connection connection = null;
        connection = getConnection();
        System.out.println(connection);
        close(connection, null, null);

    }
}
